from com.sai.ParserVM import getPage
from elementtree import TidyHTMLTreeBuilder
from xml.etree import ElementTree
import datetime
import json
import os
import pickle
import re
import sqlite3
import sys
import xml.dom.minidom


'''
#fetch the page
response = getPage('http://www.bharatmovies.com/telugu/watch/telugu-cinemas-online-2000.htm','http://srikanth_nukala:Winter12@corp-55w-proxy.mhc:8080')

sys.stdout.write(response)

sourceXml = open("C:\sourceNewXml",'w')

sourceXml.write(response)
'''
'''
# replace new lines
sourceFile = open("C:\sourceNewXml","r")
newfile = open("C:\sourceFinalXml2",'w')

newlines = ""
i = 0

for inLine in sourceFile: 
    inLine = inLine.replace('\r','')
    inLine = inLine.replace('\n','')   
    line = inLine.replace('\n\r','')
    newfile.write(line)

'''



# 
##tidy up data
#tidyData = TidyHTMLTreeBuilder.parse("C:\sourceFinalXml2")
##
#tidyData.write("C:\sourceFinalXml2", "utf-8")

# parse the file and load into datamap
tidiedData = open("C:\sourceFinalXml2")

doc = xml.dom.minidom.parse(tidiedData)

conn = {} #sqlite3.connect('c:\'+dbFileName)


c = conn.cursor();


for node in doc.getElementsByTagName("html:div"):
    divid = str(node.getAttribute("id"))  
    #print divid    
    if (divid == 'L1') | (divid == 'L2'):
        if node.hasChildNodes:
            childNode = node.getElementsByTagName("html:a")[0];
            
            #fetch mov name year and actor list
            movieStr = str(childNode.firstChild.data)
            
            matches = re.search('\((.*)\)',movieStr)
            
            try:
                #print matches.group(1)            
                year = matches.group(1) #no need if you get the correct group >> .lstrip("(").rstrip(")")
            except Exception:
                #print 'exception'
                year = ''
                
            quality = ''
            
            for qtag in childNode.getElementsByTagName("html:i"):
                quality = str(qtag.firstChild.data)
            
            movName = re.sub("\(.*\)","",movieStr).strip() #movieStr.replace(r"(.*)", "") 
            
            #year = movieStr.split("-")[0].strip()
            #movName = movieStr.split("-")[1].strip()    
            # fetch the link
                    
            hrefVal = str(childNode.getAttribute("href"))            
            #sys.stdout.write('%s and year %s and movname %s and quality %s and href %s' % (str(node.childNodes),year,movName,quality,hrefVal))
            innerMap = {}
            innerMap['year'] = year
            innerMap['movName'] = movName
            innerMap['quality'] = quality
            innerMap['hrefVal'] = hrefVal
            try:   
                #movies (year real, title text, quality text, href text, actors text)
                print year+movName       
                c.execute("insert into movies values ('%s','%s','%s','%s','%s','%s')" % (year+movName,year,movName,quality,hrefVal,''))
                conn.commit()
            except sqlite3.IntegrityError:
                print ('duplicate movie name is: %s', (year+movName))
            except sqlite3.OperationalError:
                print ('error movie details: %s , %s , %s, %s, %s', (year+movName,year,movName,quality,hrefVal))
            
        else:
            sys.stdout.write('no child nodes')

c.execute('select * from movies')

for row in c:
    print row           

c.close()            
